﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using prohomework.Models.SqlBase;
namespace prohomework.Models.SqlClient
{
    public class SqlBaseExecutant :BaseExecutant
    {
        protected string connectionString = null;
        protected SqlConnection connection = null;
        protected SqlCommand command = null;
        protected SqlDataAdapter adapter = null;
        protected string commandText = null;


        public SqlBaseExecutant(string connectionString)
        {
            this.connectionString = connectionString;
            this.connection = new SqlConnection(connectionString);
            this.command = new SqlCommand();
            this.command.Connection = this.connection;
            this.command.CommandText = this.commandText;
            this.adapter = new SqlDataAdapter(this.commandText, this.connection);
        }

        public CommandType CommandType
        {
            set { this.command.CommandType = value; }
        }
        public string ConnectionString
        {
            get
            {
                return connectionString;
            }
            set
            {
                connectionString = value;
                this.connection = new SqlConnection(value);
            }
        }

        public string CommandText
        {
            get
            {
                return this.commandText;
            }
            set
            {
                this.commandText = value;
                this.command.CommandText = value;
                //this.adapter.SelectCommand = value;
            }
        }

        public SqlParameterCollection Parameters
        {
            get { return this.command.Parameters; }
        }

        #region actions

        public override int Act(out int rowsAffected)
        {
            int returnValue = 0;
            this.BuildReturnCommand();
            this.connection.Open();
            rowsAffected = this.command.ExecuteNonQuery();
            returnValue = (int)this.command.Parameters["ReturnValue"].Value;
            this.connection.Close();
            this.ClearCommand();
            return returnValue;
        }
        public void CloseConnection()
        {
            this.connection.Close();
        }

        public override int Act(out Object result)
        {
            int returnValue = 0;
            this.BuildReturnCommand();
            this.connection.Open();
            result = this.command.ExecuteScalar();
            returnValue = (int)this.command.Parameters["ReturnValue"].Value;
            this.connection.Close();
            this.ClearCommand();
            return returnValue;
        }
        public void Act(out SqlDataReader reader)
        {
            //int returnValue = 0;
            //这是什么意思
            BuildReturnCommand();
            this.connection.Open();
            reader = this.command.ExecuteReader(CommandBehavior.CloseConnection);
            ClearCommand();
        }
        public void Act(CommandBehavior commandBehavior, out SqlDataReader reader)
        {
            //int returnValue = 0;
            BuildReturnCommand();
            this.connection.Open();
            reader = this.command.ExecuteReader(commandBehavior);
            ClearCommand();
        }
        public override int Act(string tablename, out DataSet ds)
        {
            int returnValue = 0;
            this.BuildReturnCommand();
            this.connection.Open();
            DataSet tempDS = new DataSet();
            SqlDataAdapter dataAdapter = new SqlDataAdapter(this.command);
            dataAdapter.Fill(tempDS, tablename);
            this.connection.Close();
            ds = tempDS;
            returnValue = (int)this.command.Parameters["ReturnValue"].Value;
            this.ClearCommand();
            return returnValue;
        }

        public override int Act(string tablename, DataSet ds)
        {
            int returnValue = 0;
            this.BuildReturnCommand();
            this.connection.Open();
            SqlDataAdapter dataAdapter = new SqlDataAdapter(this.command);
            dataAdapter.Fill(ds, tablename);
            this.connection.Close();
            returnValue = (int)this.command.Parameters["ReturnValue"].Value;
            this.ClearCommand();
            return returnValue;
        }

        #endregion

        private void BuildReturnCommand()
        {
            this.command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4,
                ParameterDirection.ReturnValue, false, 0, 0, String.Empty, DataRowVersion.Default, null));
        }
        private void ClearCommand()
        {
            this.command.CommandText = "";
            this.command.Parameters.Clear();
        }
    }
}